package io.lkt.modules.sys.service;

import io.lkt.common.utils.DateUtil;
import io.lkt.modules.sys.dao.BPlatformBeginnerArticleTypeDao;
import io.lkt.modules.sys.entity.BPlatformBeginnerArticleTypeEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;

import io.lkt.modules.sys.dao.BBeginnerArticleTypeDao;
import io.lkt.modules.sys.entity.BBeginnerArticleTypeEntity;
import io.lkt.modules.sys.service.BBeginnerArticleTypeService;
import org.springframework.transaction.annotation.Transactional;


@Service("bBeginnerArticleTypeService")
public class BBeginnerArticleTypeServiceImpl extends ServiceImpl<BBeginnerArticleTypeDao, BBeginnerArticleTypeEntity> implements BBeginnerArticleTypeService {
    @Autowired
    private BPlatformBeginnerArticleTypeDao bPlatformBeginnerArticleTypeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BBeginnerArticleTypeEntity> page = this.page(
                new Query<BBeginnerArticleTypeEntity>().getPage(params),
                new QueryWrapper<BBeginnerArticleTypeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BBeginnerArticleTypeEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    @Override
    public int queryListTotal(Map<String, Object> params) {
        return baseMapper.queryListTotal(params);
    }

    /**
     * 事务处理
     * @param bBeginnerArticleType
     */
    @Override
    @Transactional
    public void saveObject(BBeginnerArticleTypeEntity bBeginnerArticleType) {
        // 1、插入类型数据
        bBeginnerArticleType.setCreateTime(DateUtil.getCurDate());
        baseMapper.saveObject(bBeginnerArticleType);
        System.out.println("[bBeginnerArticleType] = " + bBeginnerArticleType);

        // 2、插入平台类型数据
        List<String> gzhCodeList = bBeginnerArticleType.getGzhCodeList();
        if (CollectionUtils.isNotEmpty(gzhCodeList)) {
            gzhCodeList.forEach((gzhCode) -> {
                BPlatformBeginnerArticleTypeEntity bPlatformBeginnerArticleTypeEntity = new BPlatformBeginnerArticleTypeEntity();
                bPlatformBeginnerArticleTypeEntity.setArticleTypeId(bBeginnerArticleType.getId());
                bPlatformBeginnerArticleTypeEntity.setGzhCode(gzhCode);
                bPlatformBeginnerArticleTypeEntity.setSort(Integer.valueOf(1));
                bPlatformBeginnerArticleTypeEntity.setCreateTime(DateUtil.getCurDate());

                bPlatformBeginnerArticleTypeDao.saveObject(bPlatformBeginnerArticleTypeEntity);
            });
        }
    }

}
